using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TeamCommunitySolution.Domain.DataLayer.Venues;
using log4net;
using TeamCommunitySolution.Services.Managers;
using TeamCommunitySolution.Services.Managers.Impl;
using TeamCommunitySolution.Services.Services;

namespace TeamCommunitySolution.Controllers
{
    /// <summary>
    /// The venue controller.
    /// </summary>
    [Authorize]
    public class VenueController : Controller
    {
        /// <summary>
        /// Logger instance
        /// </summary>
        private static readonly ILog _log = LogManager.GetLogger(typeof(VenueController));

        /// <summary>
        /// User manager instance.
        /// </summary>
        private IApplicationService _service = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="VenueController"/> class.
        /// </summary>
        public VenueController()
          : this(MvcApplication.ApplicationService)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VenueController"/> class.
        /// </summary>
        /// <param name="userManager">The user manager.</param>
        /// <param name="venueManager">The venue manager.</param>
        public VenueController(IApplicationService service)
        {
            this._service = service;
        }

        /// <summary>
        /// Indexes this instance. GET: /Venue/
        /// </summary>
        /// <returns>Action result.</returns>
        public ActionResult Index()
        {
            return View();
        }
        
        /// <summary>
        /// Detailses the specified id. GET: /Venue/Details/5
        /// </summary>
        /// <param name="id">The venue id.</param>
        /// <returns>Action Result</returns>
        public ActionResult Details(string id)
        {
          Venue venue = this._service.VenueGetById(id);
            return View("Details", venue);
        }

        /// <summary>
        /// Creates this instance.  GET: /Venue/Create
        /// </summary>
        /// <returns>Action Instance.</returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified collection. POST: /Venue/Create
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Venue venue = new Venue();
                UpdateModel(venue, collection.ToValueProvider());
                this._service.VenueCreate(venue, _service.UserGetByUsername(User.Identity.Name));
                return View("~/Views/Shared/OperationCompleted.aspx");
            }
            catch (Exception ex)
            {
                _log.Error("Error creating the venue", ex);
                return View();
            }
        }
        
        /// <summary>
        /// Edits the specified id. GET: /Venue/Edit/5
        /// </summary>
        /// <param name="id">The venue id.</param>
        /// <returns>Action Result.</returns>
        public ActionResult Edit(string id)
        {
          return View("Edit", this._service.VenueGetById(id));
        }

        /// <summary>
        /// Edits the specified id. POST: /Venue/Edit/5
        /// </summary>
        /// <param name="id">The team id.</param>
        /// <param name="collection">The collection.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
              Venue venue = this._service.VenueGetById(id);
                UpdateModel(venue, collection.ToValueProvider());
                this._service.VenueUpdate(venue);
                return View("OperationCompleted");
            }
            catch
            {
                return View();
            }
        }
    }
}
